package repositories;

import java.util.Collection;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import domain.Auction;

@Repository
public interface AuctionRepository extends JpaRepository<Auction,Integer> {
	
	// Find all active auctions
	@Query("select a from Auction a where CURRENT_TIMESTAMP between a.startMoment and a.finishMoment")
	Collection<Auction> findAllActiveAuctions();
	
	// Find all active auctions created by customers different from the principal
	@Query("select a from Auction a where a.creator.id <> ?1 and CURRENT_TIMESTAMP between a.startMoment and a.finishMoment")
	Collection<Auction> findAllActiveAuctionsCreatedByOtherCustomers(int customerId);
	
	// Find all past auctions
	@Query("select a from Auction a where CURRENT_TIMESTAMP > a.finishMoment")
	Collection<Auction> findPastAuctions();
	
	// Find past auctions by principal
	@Query("select a from Auction a where a.creator.id = ?1 and CURRENT_TIMESTAMP > a.finishMoment")
	Collection<Auction> findPastAuctionsByPrincipal(int customerId);

	// Find an active auction by principal
	@Query("select a from Auction a where a.creator.id = ?1 and CURRENT_TIMESTAMP between a.startMoment and a.finishMoment")
	Auction findActiveAuctionByPrincipal(int customerId);
	
	// Find the auction with more bids
	@Query("select a from Auction a where a.bids.size = (select max(aAux.bids.size) from Auction aAux)")
	Collection<Auction> findAuctionWithMoreBids();
}
